<?php

class Payment extends BasePayment {
	
	public function countDateExpire($level, $period) {
		if ($level == 3) {
			$c = new Criteria ( );
			$c->add ( PaymentPeer::USER_ID, sfContext::getInstance ()->getUser ()->getAttribute ( 'user_id', null, 'sfGuardSecurityUser' ) );
			$c->addAnd ( PaymentPeer::LEVEL_ID, $level );
			$c->addDescendingOrderByColumn ( PaymentPeer::DATE_EXPIRE );
			$dts = PaymentPeer::doSelectOne ( $c );
			if ($dts) {
				$now = new sfDate($dts->getDateExpire());				
			} else {
				$now = new sfDate();
			}

			return $now->addDay($period*30)->dump();
			
		}
		
		
		if ($level == 4) {
			$c = new Criteria ( );
			$c->add ( PaymentPeer::USER_ID, sfContext::getInstance ()->getUser ()->getAttribute ( 'user_id', null, 'sfGuardSecurityUser' ) );
			$c->addAnd ( PaymentPeer::LEVEL_ID, $level );
			$c->addDescendingOrderByColumn ( PaymentPeer::DATE_EXPIRE );
			$dts = PaymentPeer::doSelectOne ( $c );
			if ($dts) {
				$now = new sfDate($dts->getDateExpire());				
			} else {
				$now = new sfDate();
			}

			return $now->addDay($period)->dump();
		}
		
		////VIP Услуги
			if ($level == 12) {
			$c = new Criteria ( );
			$c->add ( PaymentPeer::USER_ID, sfContext::getInstance ()->getUser ()->getAttribute ( 'user_id', null, 'sfGuardSecurityUser' ) );
			$c->addAnd ( PaymentPeer::LEVEL_ID, $level );
			$c->addDescendingOrderByColumn ( PaymentPeer::DATE_EXPIRE );
			$dts = PaymentPeer::doSelectOne ( $c );
			if ($dts) {
				$now = new sfDate($dts->getDateExpire());				
			} else {
				$now = new sfDate();
			}

			return $now->addDay($period)->dump();
			
		}
		
	
	}
	
	public function getDirectionStr() {

		if($this->direction==1)
		{
			return __('приход');
		}
		else 
		{
			return __('расход');
		}
	}

        protected function doSave(PropelPDO $con)
	{
            $user = sfGuardUserPeer::retrieveByPK($this->user_id);
            if($user->getPartnerId()>0)
                {
                    $this->setPartnerId($user->getPartnerId());
                }
            

           ///Определяем комиссию
           $level = $this->getLevelpayment();
           if($level)
             {
               //$this->setKomission(($this->summa/100)*$level->getCost());
                 $this->setKomission($level->getCost()/100);
               parent::doSave($con);
             }

        }

}
